home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7214 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  57 lines

  1. Newsgroups: comp.lang.c
  2. Path: peer-news.britain.eu.net!warwick!bsmail!talisker!nathan
  3. From: nathan@pact.srf.ac.uk (Nathan Sidwell)
  4. Subject: Re: C constant expression declarations
  5. Message-ID: <DMto56.Lo3@uns.bris.ac.uk>
  6. Sender: usenet@uns.bris.ac.uk (Usenet news owner)
  7. Nntp-Posting-Host: talisker.pact.srf.ac.uk
  8. Organization: Inmos
  9. X-Newsreader: TIN [version 1.2 PL2]
  10. References: <31229735.41C67EA6@isi.com>
  11. Date: Thu, 15 Feb 1996 14:55:54 GMT
  12.  
  13. J.R. Stoner (jstoner@isi.com) wrote:
  14. : This is a query (I have checked the FAQ) into something that strikes me
  15. : as a dumbfoundedly stupid issue, but I will ask it anyway :)
  16.  
  17. : Normally, I will do things such as:
  18.  
  19. :  #define EXPR1   1
  20. :  #define EXPR2   2
  21.  
  22. : ...and so on.  Lately, I have been observing in code from other people
  23. : equivalent declarations such as:
  24.  
  25. :  #define EXPR1   (1)
  26. :  #define EXPR2   (2)
  27.  
  28. Whilst, as I'm sure you realise, it's necessary to put brackets round
  29. things like (a + b), it is not necessary around unsigned constants.
  30. However, a gotcha is using signed constants, for example
  31.  
  32. #define    EXPR -1
  33. if(a EXPR) ...
  34.  
  35. this will complile as 'a - 1', rather than give a syntax error
  36.  
  37. leaving out the brackets, will not affect correct code, but can cause
  38. incorrect code to be compiled to something undesired, rather than give
  39. an error. Having brackets o unsigned constants, just reinforces the habit.
  40.  
  41. Even wih the brackets, I could still have silent error, for instance
  42.  
  43. #define EXPR (-1)
  44. int a(int);
  45. if(a EXPR) ...
  46.  
  47. will be compiled as 'a(-1)'. However this compounds two errors, using
  48. a function name instead of a variable (I assume you meant to compare
  49. a variable with a value), and omit the comparison operator.
  50.  
  51. nathan
  52. --
  53. Nathan Sidwell                         Holder of the Xmris home page
  54. Chameleon Architecture Group at SGS-Thomson, formerly Inmos
  55. http://www.pact.srf.ac.uk/~nathan/                  Tel 0117 9707182
  56. nathan@inmos.co.uk or nathan@bristol.st.com or nathan@pact.srf.ac.uk
  57.